from abc import ABC, abstractmethod
from typing import List, Dict, Any, Optional, Callable
import json


class BaseDeduper(ABC):
    """
    去重算法的基类，定义统一的接口
    """
    
    def __init__(self, texts: List[str], **kwargs):
        """
        初始化去重器
        
        Args:
            texts: 待去重的文本列表
            **kwargs: 算法特定的参数
        """
        self.texts = texts
        self.kwargs = kwargs
        self.progress_callback = None
        self.duplicate_pairs = []
        
    def set_progress_callback(self, callback: Callable):
        """设置进度回调函数"""
        self.progress_callback = callback
        
    @abstractmethod
    def execute(self) -> List[str]:
        """
        执行去重操作
        
        Returns:
            去重后的文本列表
        """
        pass
        
    def get_duplicate_pairs(self) -> List[Dict[str, Any]]:
        """
        获取检测到的重复对信息
        
        Returns:
            重复对列表，每个元素包含文档ID、相似度等信息
        """
        return self.duplicate_pairs
        
    def get_stats(self) -> Dict[str, Any]:
        """
        获取去重统计信息
        
        Returns:
            包含各种统计信息的字典
        """
        result = self.execute()
        return {
            'original_count': len(self.texts),
            'deduped_count': len(result),
            'duplicate_count': len(self.texts) - len(result),
            'duplicate_pairs_count': len(self.duplicate_pairs)
        }


class DedupMethodRegistry:
    """
    去重方法注册表，管理所有可用的去重算法
    """
    
    _methods = {}
    
    @classmethod
    def register(cls, name: str, deduper_class):
        """注册去重方法"""
        cls._methods[name] = deduper_class
        
    @classmethod
    def get_method(cls, name: str):
        """获取去重方法类"""
        return cls._methods.get(name)
        
    @classmethod
    def list_methods(cls) -> List[str]:
        """列出所有可用的去重方法"""
        return list(cls._methods.keys())
        
    @classmethod
    def get_method_info(cls, name: str) -> Dict[str, Any]:
        """获取去重方法的详细信息"""
        deduper_class = cls.get_method(name)
        if deduper_class:
            return {
                'name': name,
                'description': getattr(deduper_class, 'DESCRIPTION', ''),
                'parameters': getattr(deduper_class, 'PARAMETERS', {}),
                'default_params': getattr(deduper_class, 'DEFAULT_PARAMS', {})
            }
        return None
